function wtf = seriald()
%% Using MATLAB with Serial Devices (such as RS-232)
% MATLAB supports communication with serial devices including RS-232 when 
% using the Instrument Control Toolbox.  There are many devices with serial
% interfaces, including gas chronometers, mass spectrometers, 
% imaging devices, pulse oximeters, and instruments.
%
% The toolbox provides a graphical tool that allows you to configure, 
% control, and acquire data from your serial device without writing code. 
% The tool automatically generates MATLAB code that allows you to reuse
% your work.  The code example below was automatically generated by the
% tool.  Type "tmtool" at the MATLAB command line to launch this tool.
%
% This code example below shows you how you can communicate with your
% serial device using MATLAB.  The "*IDN?" command was used which is a 
% typical command for communicating with an instrument.  The commands you
% can use will depend on what your serial device supports.

%% Automatically generating a report in MATLAB
% Press the "Save and Publish to HTML" button in the MATLAB Editor to 
% execute this code example and automatically generate a report of your 
% work with the serial device.

%% Automatically generating MATLAB script for your RS-232 device
% To automatically create your own MATLAB script, launch "tmtool". Open the
% "Hardware" node, open the "Serial" node, select your serial port (such
% as COM1, press the "connect" button.  Once connected, enter your device 
% commands in the right pane, press "Session log" to see the code generated,
% and press "Save Session" to save the code to a MATLAB (.m) file.

%% MATLAB script automatically generated for the RS-232 device
% The following MATLAB script was automatically generated by interacting
% with the device configuration tool provided by the toolbox.  

% Creation time: 03-Oct-2006 20:36:43

% Create a serial port object.
obj1 = instrfind('Type', 'serial', 'Port', 'COM1', 'Tag', '');

% Create the serial port object if it does not exist
% otherwise use the object that was found.
if isempty(obj1)
    obj1 = serial('COM1', 'BaudRate',115200);
else
    fclose(obj1);
    obj1 = obj1(1)
end

% Connect to instrument object, obj1.
fopen(obj1);
%fseek(obj1,0,1);

fid = fopen('capture.txt','w'); 

buffer = ones(500, 8);

% Communicating with instrument object, obj1.
%data1 = query(obj1, '*IDN?');
%figure;
% hold;
if(obj1.BytesAvailable > 0) 
    fread(obj1, obj1.BytesAvailable);
end

while 1
   %data1 = fgets(obj1);
    if(obj1.BytesAvailable > 0) %clear out buffer
        null = fread(obj1, obj1.BytesAvailable);
    end
    
   data1=0;
   
   while(data1 < 8)
    data1 = fread(obj1, 20); %integer
   end
   
   %data1 = fscanf(obj1,'%s');
    if data1 > 0
        while ~isempty(data1)
            if length(data1) <= 2
                break 
            end
                
            if data1(1) == hex2dec('AA') && data1(2) == hex2dec('BB')
               % disp('data match');
               data1 = data1(3:end-2);
                if length(data1) >= 8
                    
                    fprintf(fid, '%d, ' ,data1(1:7));
                    fprintf(fid, '%d' ,  data1(8));
                    fprintf(fid, '\n');
                   
                  %  plot([1:1:8], data1(1:8));
                    %stem([1:1:8],data1(1:8))
                    data1(1:8);
                    buffer=buffer([2:end 1], :);
                    buffer(end, :) = 3.3*data1(1:8)/256;
                    pause(0.0002);
    
                    for k = 1:8
                        subplot(8,1,k)
                        axis([1 length(buffer) 0 3.3])
                        plot(1:1:length(buffer), buffer(1:end, k))
                    end
                    %null = fread(obj1, 512); %integer

                    data1 = 0;
                    
                    break;
                end
                
            end
            data1 = data1([2:end]);
        end
       % data1
       %fprintf(fid,'%d, ' ,data1);
      % fprintf(fid,'\n');
    end
       
end
% Disconnect from instrument object, obj1.
fclose(obj1);

fclose(fid);
% Clean up all objects.
delete(obj1);